<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ include file = "./login.jsp" %>
<%@page import="jdbc.JdbcDao"%>
<%@page import="edu.thu.cs.*"%>

<%
request.setCharacterEncoding("utf-8");
String hidehtml = request.getParameter("hidehtml");

if (!"true".equals(hidehtml)) {

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Insert title here</title>
    <style type="text/css">
    body {
    color: white;
    }
    </style>
</head>
<body>

<%
	}


	{
		String op = StaticFunc.encodingUtf8(request.getParameter("operator"));
		String articleid = StaticFunc.encodingUtf8(request.getParameter("articleid"));
		String title = StaticFunc.encodingUtf8(request.getParameter("title"));
		String authoremail = email_status;
		String tags = StaticFunc.encodingUtf8(request.getParameter("tags"));
		String content = StaticFunc.encodingUtf8(request.getParameter("content"));
		String commenttos = StaticFunc.encodingUtf8(request.getParameter("commentto"));
		String name = title;
		String credit = StaticFunc.encodingUtf8(request.getParameter("credit"));
		String teacher = StaticFunc.encodingUtf8(request.getParameter("teacher"));
		String snumlimit = StaticFunc.encodingUtf8(request.getParameter("numlimit"));
		String classdate = StaticFunc.encodingUtf8(request.getParameter("classdate"));
		
		System.out.println(title);
		int commentto = 0;
		int numlimit = 0;
		
		try {
		if (commenttos != null)
			commentto = Integer.parseInt(commenttos);
		} catch (Exception e) {
			commentto = 0;
		}
		
		try {
		if (snumlimit != null)
			numlimit = Integer.parseInt(snumlimit);
		} catch (Exception e) {
			numlimit = 0;
		}

		JdbcDao mysql = new JdbcDao();
		if ("postArticle".equals(op)) {
			if (email_status == null)
				out.print("请登录后操作.");
			else {
				if (tags == null)
					tags = "";
				mysql.postArticle(title, authoremail, content, tags, commentto);
				out.print("文章发表成功.");
			}
		}
		if ("getArticleInfo".equals(op)) {
			String s = StaticFunc.toJson(mysql.getArticleInfo(Integer.parseInt(articleid)));
			out.print(s);
		}
		if ("getArticleList".equals(op)) {
			String s = StaticFunc.toJson(mysql.getArticleList());
			out.print(s);
		}
		if ("searchArticleByCommentto".equals(op)) {
			String s = StaticFunc.toJson(mysql.searchArticleByCommentto(commentto));
			out.print(s);
		}
		if ("searchArticleByTag".equals(op)) {
			String s = StaticFunc.toJson(mysql.searchArticleByTag(tags));
			out.print(s);
		}
		if ("delReply".equals(op)) {
			boolean s = mysql.delReply(Integer.parseInt(articleid));
			if (s)
				out.print("删除文章成功");
			else
				out.print("删除文章失败");
		}
		if ("editArticle".equals(op)) {
			int id = 0;
			if (articleid != null)
				id = Integer.parseInt(articleid);

			mysql.changeArticleInfo(id, "title", title);
			mysql.changeArticleInfo(id, "tags", tags);
			mysql.changeArticleInfo(id, "content", content);
			mysql.changeArticleInfo(id, "commentto", commenttos);
			
			out.print("修改文章成功");
		}
		if ("newClass".equals(op)) {
			if (email_status == null)
				out.print("请登录后操作.");
			else {
				if (tags == null)
					tags = "";
				int classid = 
						mysql.postArticle("课程介绍: " + title, authoremail, content, tags, commentto);
				if (classid == -1)
					out.print("新建课程失败.");
				else {
					if (mysql.newClass(classid + "", name, credit, teacher, numlimit, classdate))
						out.print("新建课程成功.");
					else
						out.print("新建课程失败.");
				}
			}
		}
	}

	if (!"true".equals(hidehtml)) {
%>

</body>
</html>

<% } %>